<template>
  <div class="app-container clearfix">
    <!-- 搜索 -->
    <salary-search
      :init-params="initParams"
      :search-params="searchKey"
      :department-all-list-data="departmentAllListData"
      @on-init="handleInitList"></salary-search>
    <!-- 列表 -->
    <salary-table
      :data="listData"
      :search-params="searchKey"
      :total-count="totalCount"
      :list-loading="listLoading"
      @on-init="handleInitList"
      @on-edit="handleEditShow"
      @on-delete="handleDeleteShow"
      @on-back="handleBackShow"
      @on-view="handleViewShow"></salary-table>
    <!-- 编辑弹出框 -->
    <salary-make
      :visible.sync="editInfo.visible"
      :department-all-list-data="departmentAllListData"
      @on-created="handleCreated"></salary-make>
    <!-- 撤回审批弹出框 -->
    <salary-back
      :visible.sync="backInfo.visible"
      :detail-data="backInfo.detailInfo"
      @on-init="handleInitList"></salary-back>
  </div>
</template>

<script>
  import { getSalaryListAPI } from '@/api/todo/salary'
  import { SalarySearch, SalaryTable, SalaryMake, SalaryBack } from './components'
  import treeAllListData from '@/mixins/treeAllListData'
  import request from '@/utils/request'
  import {
    getPiaoListAPI,
    getPiaoListUpdateAPI,
    savePiaomakeAPI,
    submitApproveAPI,
    deleteApproveAPI,
    getInvoiceTreeAPI,
    drawbackAPI
  } from '@/api/todo/piaomake'

  export default {
    name: 'SalaryList',
    mixins: [treeAllListData],
    components: {
      SalarySearch,
      SalaryTable,
      SalaryMake,
      SalaryBack
    },
    data() {
      return {
        initParams: { // 初始化参数
          pageNum: 1, // 当前页数
          pageSize: 10, // 每页个数
          // sort: '', // 排序

          projectName: '', 
          projectNo: '' ,
          contractNo: ''

        },
        searchKey: {}, // 搜索参数
        editInfo: { // 编辑弹出框
          visible: false, // 是否显示模态框
          detailInfo: {}
        },
        backInfo: { // 撤回弹出框
          visible: false, // 是否显示模态框
          detailInfo: {}
        },
        totalCount: 0, // 分页用的列表总条数
        listLoading: false,
        listData: [] // 列表数据
      }
    },
    props: {},
    computed: {},
    methods: {
    	goSalaryreissue({ type, data }) { // 进入工资补发
        let query = { type }
        if (type === '1') {
          query = {
            ...query,
            ...data
          }
        } else {
          query['id'] = data.id
        }
        //console.log(query)
        this.$router.push({
          name: 'Salaryreissue',
          query
        })
      },
      goPiaomak({ type, data }) { // 进入开票制作
        let query = { type }
        if (type == '1') {
          query = {
            ...query,
            ...data
          }
        } else {
          query['id'] = data.id
        }
        //console.log(query)
        this.$router.push({
          name: 'Piaomake',
          query
        })
      },

      handleDeleteShow(data = {}) { 
        this.$confirm(`是否确认删除项目:${data.projectName}的开票申请?`, '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
                  request({
                      url: `/hr/invoice/apply/delete/${data.id}`,
                      method: 'delete',
                      params: {
                         
                      }
                  }).then(response => {
                      if (response.code != '200') {
                          this.msgError('删除失败，原因是' + response.data.msg)
                          return
                      }
                       this.$notify({
                          title: '成功',
                          message: '删除成功！',
                          type: 'success',
                          duration: 2000
                        })

                         this.getListData()

                  })

            }).catch(() => {
            })
        
      },

      handleEditShow(data = {}) { // 编辑－显示弹出框
        
        if(!data){
          this.goPiaomak({
            type: '1', 
            data
          });
        }else{
          this.goPiaomak({
            type: '2', 
            data
          });
        }

        

      },
      handleBackShow(data = {}) { // 撤回审批－显示弹出框
        this.backInfo.visible = true
        this.backInfo.detailInfo = data
      },
      handleViewShow(data = {}) {
        if (data) {
          this.goPiaomak({
            type: '3',
            data
          })
        }
      },
      handleCreated(data = {}) { // 添加
      	console.log('页面跳转之前的参数为：',data);
      	//跳工资补发页面
      	if(data.salaryType == 2){

      		this.goSalaryreissue({
      			 type: '1',
          		data
      		})
      		return
      	}

      	//跳月薪制作页面
        this.goPiaomak({
          type: '1',
          data
        })
      },
      handleUpdated() { // 编辑成功
        this.getListData()
      },
      handleInitList(params = {}) { // 初始化列表
        this.searchKey = {
          ...this.searchKey,
          ...params
        }
        this.getListData()
      },
      async getDepartmentAllListData() { // 获取所有数据
        try {
          await this.$store.dispatch('department/GetDepartmentList', {
            request: true,
            showChargedOnly: true
          })
        } catch (error) {
          // console.log(error)
          this.$notify.error({
            title: '错误',
            message: error.message
          })
        }
      },
      async getListData() { // 获取列表数据
        try {
          let { searchKey } = this
          this.listLoading = true
          let response = await getPiaoListAPI({
            ...searchKey
          })
          let result = response
          if (+result.code === 200) {
            this.listData = result.rows || []
            this.totalCount = result.total || 0
          } else {
            this.$notify.error({
              title: '错误',
              message: result.msg || '获取列表失败！'
            })
          }
        } catch (error) {
          // console.log(error)
          this.$notify.error({
            title: '错误',
            message: error.message || '获取列表失败!'
          })
        } finally {
          this.listLoading = false
        }
      },

      async initData() { // 初始化数据
        this.getListData()
        this.getDepartmentAllListData()
      }
    },
    watch: {
      initParams: {
        handler: function(newVal, oldVal) {
          if (JSON.stringify(newVal) !== JSON.stringify(this.searchKey)) {
            this.searchKey = JSON.parse(JSON.stringify(newVal))
          }
        },
        deep: true,
        immediate: true
      }
    },
    created() {
    },
    mounted() {
      this.initData()
    }
  }
</script>

<style lang="scss" scoped>
</style>
